import Vue from "vue";
import Vuex from "vuex";
import indexAdminRoute from "@/tool/adminRoute";
Vue.use(Vuex);
// 前台状态管理
function sessionStg(state) {
  window.sessionStorage.setItem("aState", JSON.stringify(state));
}
//    获取rootSubmenuKeys
function getAllSubMenuKeyList() {
  let rootSubmenuKeys = [];
  indexAdminRoute.children.forEach(item => {
    if (item.children) {
      rootSubmenuKeys.push(item.name);
    }
  });
  return rootSubmenuKeys;
}

export default new Vuex.Store({
  state: {
    // 菜单栏
    activeHeaderMenu: ["home"], // header menu菜单
    allSubMenuKeyList: getAllSubMenuKeyList(), //所有的有子节点的menu Name
    openKey: [], // 侧边栏当前打开的菜单栏
    // 面包屑
    currentBreadcrumb: [], //当前的面包屑
    // router
    currentRouterName: "home", // 当前的Router名,
    isOperationPage: false, // 当前页面的是否为操作页面
    isLogin: false, //当前登录状态
    userInfo: {}, //个人信息
    name: "",
    spinning: false
  },
  actions: {
    changeUserInfo(context, info) {
      context.commit("changeUserInfo", info);
    },
    changeSpinning(context, isLand) {
      context.commit("changeSpinning", isLand);
    },
    changeName(context, name) {
      context.commit("changeName", name);
    },
    changeCurrentRouter(context, routerName) {
      context.commit("changeCurrentRouter", routerName);
    },
    changeActiveMenu(context, routerName) {
      context.commit("changeActiveMenu", routerName);
    },
    changeCurrentBreadcrumb(context, list) {
      context.commit("changeCurrentBreadcrumb", list);
    },
    changeOpenKey(context, list) {
      context.commit("changeOpenKey", list);
    },
    changeShowOperationPage(context, isShow) {
      context.commit("changeShowOperationPage", isShow);
    },
    changeLoginState(context, login) {
      context.commit("changeLoginState", login);
    }
  },
  mutations: {
    changeUserInfo(state, info) {
      state.userInfo = info;
      sessionStg(state);
    },
    changeSpinning(state, isLand) {
      state.spinning = isLand;
      sessionStg(state);
    },
    changeAthList(state, list) {
      state.athList = list;
      sessionStg(state);
    },
    changeName(state, name) {
      state.name = name;
      sessionStg(state);
    },
    changeCurrentRouter(state, routerName) {
      state.currentRouterName = routerName;
      sessionStg(state);
    },
    changeActiveMenu(state, routerName) {
      state.activeHeaderMenu = [routerName];
      sessionStg(state);
    },
    changeCurrentBreadcrumb(state, list) {
      state.currentBreadcrumb = list;
      sessionStg(state);
    },
    changeOpenKey(state, list) {
      state.openKey = list;
      sessionStg(state);
    },
    changeShowOperationPage(state, isShow) {
      state.isOperationPage = isShow;
      sessionStg(state);
    },
    changeLoginState(state, login) {
      state.isLogin = login;
      sessionStg(state);
    }
  },
  getters: {}
});
